import sys
import heapq
import math
def solute():
n, k = map(int, sys.stdin.readline().strip().split())
a_list = list(map(int, sys.stdin.readline().split()))
def coprime(a, b):
return math.gcd(a, b) == 1
con_pairs = []
con_ones = []
special_con_ones = []
curr = 0
prev = 1
con_one = 0
divs, rems = 0, 0
for i, num in enumerate(a_list):
if con_one:
if num == 1:
con_one += 1
continue
else:
con_ones.append(con_one)
con_one = 0
prev = num
continue
if num == 1:
con_one = 1
if curr:
con_pairs.append(curr)
curr = 0
prev = 1
continue
if i == 0:
prev = num
continue
if coprime(num, prev):
curr += 1
else:
if curr:
con_pairs.append(curr)
curr = 0
prev = num
if curr:
con_pairs.append(curr)
if con_one:
rems += con_one
if all(num == 1 for num in a_list):
return max(0, con_one - k)
if a_list[0] == 1:
rems += con_ones.pop(0)
con_ones.sort()
special_con_ones.sort()
ans = 0
divs += sum(i >> 1 for i in con_pairs)
rems += sum(i & 1 for i in con_pairs)
r_divs = min(divs, k)
divs -= r_divs
k -= r_divs
for i in range(len(con_ones)):
if k >= con_ones[i]:
k -= con_ones[i]
else:
ans += con_ones[i] + 1
r_rems = min(rems, k)
rems -= r_rems
k -= r_rems
ans = ans + 2 * divs + rems
if ans and k:
ans = max(0, ans - k)
return ans
t = int(sys.stdin.readline())
for _ in range(t):
print(solute())
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
constexpr int MX = 1e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd() { return rng() % MX; }
const int MN = 2e5 + 5;
int n, k, a[MN];
int main() {
int T; scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &k);
bool all_one = true;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
all_one &= (a[i] == 1);
}
if (all_one) {
if (k >= n) printf("0\n");
else printf("%d\n", n - k);
continue;
}
int ans = 0, d1 = 0, d2 = 0;
for (int i = 1; i < n; ++i)
ans += __gcd(a[i], a[i + 1]) == 1;
vector < int > one = {};
for (int i = 1, j; i <= n; i = j + 1) {
j = i;
if (a[i] != 1) {
while (j < n && __gcd(a[j], a[j + 1]) == 1 && a[j + 1] != 1)
++j;
if (i == j) continue;
d2 += (j - i) / 2;
d1 += (j - i + 1) % 2 == 0;
} else {
while (j < n && a[j + 1] == 1)
++j;
if (i == 1 || j == n) d1 += j - i + 1;
else one.emplace_back(j - i + 1);
}
}
ans -= min(k, d2) * 2;
k -= min(k, d2);
sort(one.begin(), one.end());
for (int len : one)
if (k >= len) {
ans -= len + 1;
k -= len;
} else {
ans -= k;
k = 0;
break;
}
ans -= min(k, d1);
printf("%d\n", ans);
}
return 0;
}
1632C - Strange Test | 673A - Bear and Game |
276A - Lunch Rush | 1205A - Almost Equal |
1020B - Badge | 1353A - Most Unstable Array |
770A - New Password | 1646B - Quality vs Quantity |
80A - Panoramix's Prediction | 1354B - Ternary String |
122B - Lucky Substring | 266B - Queue at the School |
1490A - Dense Array | 1650B - DIV + MOD |
1549B - Gregor and the Pawn Game | 553A - Kyoya and Colored Balls |
1364A - XXXXX | 1499B - Binary Removals |
1569C - Jury Meeting | 108A - Palindromic Times |
46A - Ball Game | 114A - Cifera |
776A - A Serial Killer | 25B - Phone numbers |
1633C - Kill the Monster | 1611A - Make Even |
1030B - Vasya and Cornfield | 1631A - Min Max Swap |
1296B - Food Buying | 133A - HQ9+ |